<?php

namespace app\order\logic;

use app\data\util\HttpUtil;
use think\facade\Db;
use think\facade\Cache;
use util\Excel;
use think\exception\ValidateException;
use think\facade\Log;
use util\Token;
use util\Upload;

class OrderCheckLogic
{
    protected $model = null;
    public function __construct()
    {
        $this->model = new \app\order\model\OrderCheck();
    }

    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 列表
     * @author zhangheng
     */
    public function index($params = [])
    {
        $date = $params['date'] ?? [];
        $type = $params['type'] ?? [];
        $limit = $params['limit'] ?? 10;
        $where = [];
        
        if(!empty($type)){
            $where['type'] = $type;
        }
        if(!empty($date)){
            $where['date'] = $date;
        }

        $list = Db::name('ordercheck')
            ->where($where)
            ->order("date desc")
            ->paginate($limit);

        $lists = $list->items();
        foreach ($lists as $k=>$v) {
            switch ($v['status']) {
                case '0':
                    $lists[$k]['status'] = '匹配中';
                    break;
                case '1':
                    $lists[$k]['status'] = '已完成';
                    break;
                default:
                    $lists[$k]['status'] = '匹配中';
            }
            switch ($v['type']) {
                case '1':
                    $lists[$k]['type'] = '争议成本';

                    //统计
                    $lists[$k]['count'] = Db::name('ordercheck_dispute')->where('ordercheck_id',$v['id'])->count();
                    
                    break;
                case '2':
                    $lists[$k]['type'] = '物流成本';
                    
                    //统计
                    $lists[$k]['count'] = Db::name('ordercheck_logistics')->where('ordercheck_id',$v['id'])->count();

                    break;
                case '3':
                    $lists[$k]['type'] = '采购成本';

                    //统计
                    $lists[$k]['count'] = Db::name('ordercheck_skucost')->where('ordercheck_id',$v['id'])->count();

                    break;
                default:
                    $lists[$k]['type'] = '未知';
            }
        }
                
        $data = [
            'list' => $lists,
            'total' => $list->total(),
        ];
        return json(DefaultResponse(200, true, '获取成功', $data));
    }

    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 导出
     * @author zhangheng
     */
    public function export($params = [])
    {
        $lists = [];
        $header = [];
        // $header[] = ['广告账户', 'advertiser_id'];
        $fileName = "export_Advaccount" . time();
        return Excel::exportData($lists, $header, $fileName, 'xlsx');
    }

    public function add($data = [])
    {
        if (!$data) {
            return json(DefaultResponse(201, false, '参数不能为空!', null));
        }
        if (!$data['date']) {
            return json(DefaultResponse(201, false, '请填写月份!', null));
        }
        if (!$data['type']) {
            return json(DefaultResponse(201, false, '请填写类型!', null));
        }
        $row = Db::name('ordercheck')
            ->where('date',$data['date'])
            ->where('type',$data['type'])
            ->find();
        if ($row) {
            return json(DefaultResponse(201, false, '已存在任务!', null));
        }

        $result = false;

        try {
            //广告账户
            $ordercheck['date'] = $data['date'];
            $ordercheck['time'] = strtotime($data['date']);
            $ordercheck['type'] = $data['type'];
            $ordercheck['status'] = $data['status'] ?? 0;

            $result = Db::name('ordercheck')->insert($ordercheck);
            if ($result !== false) {
                return json(DefaultResponse(200, true, '添加成功!', null));
            } else {
                return json(DefaultResponse(201, false, '添加失败!', null));
            }
        } catch (ValidateException $e) {
            return json(DefaultResponse(201, false, $e->getError(), null));
        } catch (\Exception $e) {
            return json(DefaultResponse(201, false, '广告账户已存在!', null));
        }
        return json(DefaultResponse(200, false, '更新成功!', null));

    }

    public function find($id)
    {
        if (!$id) {
            return json(DefaultResponse(201, false, '参数不能为空!', null));
        }
        $row = $this->model->find($id);
        
        if (empty($row)) {
            return json(DefaultResponse(201, false, '数据不存在!', null));
        }

        switch ($row['type']) {
            case '1':
                $row['type'] = '争议成本';
                break;
            case '2':
                $row['type'] = '物流成本';
                break;
            case '3':
                $row['type'] = '采购成本';
                break;
            default:
                $row['type'] = '未知';
        }

        return json(DefaultResponse(200, true, '获取成功', $row));
    }

    public function edit($data = [])
    {
        if (!$data) {
            return json(DefaultResponse(201, false, '参数不能为空!', null));
        }
        $id = $data['id'];
        $row = $this->model->find($id);
        if (empty($row)) {
            return json(DefaultResponse(201, false, '数据不存在!', null));
        }
        $result = false;

        try {
            
            //广告账户
            $ordercheck['status'] = $data['status'] ?? 0;
            $result = $row->save($ordercheck);

            if ($result) {
                return json(DefaultResponse(200, false, '更新成功!', null));
            }
            return json(DefaultResponse(201, false, '更新失败!', null));
        } catch (ValidateException $e) {
            return json(DefaultResponse(201, false, $e->getError(), null));
        } catch (\Exception $e) {
            return json(DefaultResponse(201, false, '更新失败!', null));
        }
        return json(DefaultResponse(200, false, '更新成功!', null));
    }
    
    
}
